﻿using System.Web.Mvc;
using Sklep.WebUI.Infrastructure.Abstract;
using Sklep.WebUI.Models;

namespace Sklep.WebUI.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAuthProvider authProvider;

        public AccountController(IAuthProvider auth)
        {
            authProvider = auth;
        }

        public ViewResult LogOn()
        {
            return View();
        }


        [HttpPost]
        public ActionResult LogOn(LogOnViewModel model, string returnUrl)
        {
            if(ModelState.IsValid)
            {
                if(authProvider.Authenticate(model.UserName,model.Password))
                {
                    return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
                }
                else
                {
                    ModelState.AddModelError("","Niepoprawna nazwa użytkownika lub hasło");
                    return View();
                }
            }
            else
            {
                
                return View();
            }
        }
    }
}
